﻿var editor;
var app = angular.module('56lea', []);
var agScope = {};
var msgNum = 0;
app.controller('56leaControl', ["$scope","$http", function($scope,$http) {
	var div = $('#auto-id-1471335680171');
	$scope.msgList=[];
	$scope.onSend = function() {
		$.logTip("sendding msg......");
		editor.sync();
		var html = $('#kedit').val();
		html=html.replace("<img src=\"http://127.0.0.1:8020/WebChat/plugins/emoticons/images/","(pq_");
		html=html.replace("\" border=\"0\" alt=\"\" />","_pq)");
/*	单机测试
	$scope.data.msgList.push({type:1,content:html,icon:'client.png'});
	$scope.$apply();
	div.scrollTop(document.getElementById('auto-id-1471335680171').scrollHeight);
	editor.html("");
*/
		sendSingleTextMsg(html, "1c19U4300000005", function(state, res) {
			if(state) {
				$.logData('send ok:' + JSON.stringify(res));
				res.user_id = res.from_user_id;
				res.msg_data = Base64.encode(res.msg_data);
				res.type = res.msg_type;
				res.session_id = res.to_session_id;
				html=html.replace("(pq_","<img src=\"http://127.0.0.1:8020/WebChat/plugins/emoticons/images/");
				html=html.replace("_pq)","\" border=\"0\" alt=\"\" />");
				$scope.msgList.push({from:$scope.usn,content:html,icon:'client.png'});
				$scope.$apply();
				//var key = currentSession.session_type + '_' + currentSession.session_id;
				//imDb.addMessagetoDb(key,res);
				div.scrollTop(document.getElementById('auto-id-1471335680171').scrollHeight);
				msgNum++;
				editor.html("");
			} else {
				console.log('send failed');
			}
		});
	}
	
	$http.get("http://192.168.1.199/ser/",null).success(initSocket).error(function(data, status) {
        	$.logTip("error:"+JSON.stringify(data));
    });
		//$('#auto-id-1471335680171').append("<div class='msg msg_left f-cb'><i class='u-icon-avater'><img src='img/company.png'></i><div class='text'><div class='arrow'></div><p>这里是人工客服，很高兴为您服务。这是新消息！</p></div></div><div class='msg msg_right f-cb'><i class='u-icon-avater'><img src='img/client.png'></i><div class='image'><div class='arrow'></div><img data-group='ysf' src='img/img.jpg'></div></div>");

	//$scope.usn = "testluanma"; 
	//$scope.pwd = "a1234567";
	agScope = $scope;
}]);

app.filter('trustHtml',['$sce',function($sce){
	return function (msg){
		return $sce.trustAsHtml(msg);
	};
}])
PQEditor.ready(function(K) {
	editor = K.create('textarea[name="content"]', {
		uploadJson: './asp.net/upload_json.ashx',
		allowFileManager: false,
		afterCreate: function() {
			var self = this;
		},
		items: ['emoticons', 'image']
	});
	var div = $('#auto-id-1471335680171');
	div.scrollTop(document.getElementById('auto-id-1471335680171').scrollHeight);
});